﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Fc
{
    public partial class JinDu3_In : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime d;
            if(!DateTime.TryParse(this.RiQi.Text,out d))
            {
                if (DateTime.Now.Hour > 12)
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
                else
                {
                    RiQi.Text = DateTime.Today.AddDays(-1).ToString("yyyy年M月d日");
                }
            }
            if (this.IsPostBack)
            {

            }
            else
            {
                string id = Request.QueryString["id"];
                int billid;
                if (id == null)
                {
                }
                else if(int.TryParse(id, out billid))
                {
                    ViewState["BillID"] = billid;
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = 
@"select 日期, 产品类别ID, 工段, 原料投入根数, 原料投入重量, 切料支数, 粗车合格, 粗车不合格, 打字, 车丝1, 车丝2, 车丝3, 车丝4, 车丝5, 车丝6, 
车丝7, 车丝8, 车丝9, 车丝10, 车丝11, 车丝12, 车丝13, 车丝14, 车丝15, 车丝16, 车丝17, 车丝18, 车丝19, 车丝20, 车废1, 车废2, 车废3, 车废4, 车废5, 
车废6, 车废7, 车废8, 车废9, 车废10, 车废11, 车废12, 车废13, 车废14, 车废15, 车废16, 车废17, 车废18, 车废19, 车废20, 磁粉碳伤合格, 
磁粉碳伤不合格, 磷化支数, 边角料量
from [生产进度3] where id=" + billid;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.BanCi.SelectedValue = Reader["工段"].ToString();
                        this.ChanPin.SelectedValue = Reader["产品类别ID"].ToString();
                        this.RiQi.Text = ((DateTime)Reader["日期"]).ToLongDateString();
                        this.TouRuGen.Text = Reader["原料投入根数"].ToString();
                        this.TouRuZhong.Text = Reader["原料投入重量"].ToString();
                        this.QieLiaoZhi.Text = Reader["切料支数"].ToString();
                        this.CuChe.Text = Reader["粗车合格"].ToString();
                        this.CuCheFei.Text = Reader["粗车不合格"].ToString();
                        this.DaZi.Text = Reader["打字"].ToString();
                        this.CheSi1.Text = Reader["车丝1"].ToString();
                        this.CheSi2.Text = Reader["车丝2"].ToString();
                        this.CheSi3.Text = Reader["车丝3"].ToString();
                        this.CheSi4.Text = Reader["车丝4"].ToString();
                        this.CheSi5.Text = Reader["车丝5"].ToString();
                        this.CheSi6.Text = Reader["车丝6"].ToString();
                        this.CheSi7.Text = Reader["车丝7"].ToString();
                        this.CheSi8.Text = Reader["车丝8"].ToString();
                        this.CheSi9.Text = Reader["车丝9"].ToString();
                        this.CheSi10.Text = Reader["车丝10"].ToString();
                        this.CheSi11.Text = Reader["车丝11"].ToString();
                        this.CheSi12.Text = Reader["车丝12"].ToString();
                        this.CheSi13.Text = Reader["车丝13"].ToString();
                        this.CheSi14.Text = Reader["车丝14"].ToString();
                        this.CheSi15.Text = Reader["车丝15"].ToString();
                        this.CheSi16.Text = Reader["车丝16"].ToString();
                        this.CheSi17.Text = Reader["车丝17"].ToString();
                        this.CheSi18.Text = Reader["车丝18"].ToString();
                        this.CheSi19.Text = Reader["车丝19"].ToString();
                        this.CheSi20.Text = Reader["车丝20"].ToString();

                        this.CheFei1.Text = Reader["车废1"].ToString();
                        this.CheFei2.Text = Reader["车废2"].ToString();
                        this.CheFei3.Text = Reader["车废3"].ToString();
                        this.CheFei4.Text = Reader["车废4"].ToString();
                        this.CheFei5.Text = Reader["车废5"].ToString();
                        this.CheFei6.Text = Reader["车废6"].ToString();
                        this.CheFei7.Text = Reader["车废7"].ToString();
                        this.CheFei8.Text = Reader["车废8"].ToString();
                        this.CheFei9.Text = Reader["车废9"].ToString();
                        this.CheFei10.Text = Reader["车废10"].ToString();
                        this.CheFei11.Text = Reader["车废11"].ToString();
                        this.CheFei12.Text = Reader["车废12"].ToString();
                        this.CheFei13.Text = Reader["车废13"].ToString();
                        this.CheFei14.Text = Reader["车废14"].ToString();
                        this.CheFei15.Text = Reader["车废15"].ToString();
                        this.CheFei16.Text = Reader["车废16"].ToString();
                        this.CheFei17.Text = Reader["车废17"].ToString();
                        this.CheFei18.Text = Reader["车废18"].ToString();
                        this.CheFei19.Text = Reader["车废19"].ToString();
                        this.CheFei20.Text = Reader["车废20"].ToString();
                        this.CiTan.Text = Reader["磁粉碳伤合格"].ToString();
                        this.CiTanFei.Text = Reader["磁粉碳伤不合格"].ToString();
                        this.LinHuaZhi.Text = Reader["磷化支数"].ToString();
                        this.JiaoLiao.Text = Reader["边角料量"].ToString();
                    }
                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }

            }
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            object billid=ViewState["BillID"];

            int banci;
            if (!int.TryParse(this.BanCi.Text, out banci))
            {
                this.Message.Text = "·请选择班次";
                return;
            }
            int chanpin;
            if (!int.TryParse(this.ChanPin.Text, out chanpin))
            {
                this.Message.Text = "·请选择产品";
                return;
            }
            DateTime riqi;
            if (!DateTime.TryParse(this.RiQi.Text, out riqi))
            {
                this.Message.Text = "·输入的日期有误";
                return;
            }
            int tourugen;
            if (!int.TryParse(this.TouRuGen.Text, out tourugen))
            {
                this.Message.Text = "·原料投入根输入有误";
                return;
            }
            double touruzhong;
            if (!double.TryParse(this.TouRuZhong.Text, out touruzhong))
            {
                this.Message.Text = "·原料投入重量输入有误";
                return;
            }

            int qieliaozhi;
            if (!int.TryParse(this.QieLiaoZhi.Text, out qieliaozhi))
            {
                this.Message.Text = "·切料支数输入有误";
                return;
            }

            int cuche;
            if (!int.TryParse(this.CuChe.Text, out cuche))
            {
                this.Message.Text = "·粗车合格支数输入有误";
                return;
            }
            int cuchefei;
            if (!int.TryParse(this.CuCheFei.Text, out cuchefei))
            {
                this.Message.Text = "·粗车不合格支数输入有误";
                return;
            }

            int dazi;
            if (!int.TryParse(this.DaZi.Text, out dazi))
            {
                this.Message.Text = "·打字支数输入有误";
                return;
            }

            int citan;
            if (!int.TryParse(this.CiTan.Text, out citan))
            {
                this.Message.Text = "·磁粉碳伤合格支数输入有误";
                return;
            }
            int citanfei;
            if (!int.TryParse(this.CiTanFei.Text, out citanfei))
            {
                this.Message.Text = "·磁粉碳伤不合格支数输入有误";
                return;
            }
            int linhua;
            if (!int.TryParse(this.LinHuaZhi.Text, out linhua))
            {
                this.Message.Text = "·磷化处理支数输入有误";
                return;
            }
            double jiaoliao;
            if (!double.TryParse(this.JiaoLiao.Text, out jiaoliao))
            {
                this.Message.Text = "·边角料量输入有误";
                return;
            }

            int[] chesi = new int[20];
            int[] chefei = new int[20];
            string chesifield = "";
            string chefeifield = "";
            string chesivalue = "";
            string chefeivalue = "";
            int i;
            for (i = 1; i <= 20; i++)
            {
                if (!int.TryParse(((TextBox)this.FindControl("CheSi"+i)).Text, out chesi[i-1]))
                {
                    this.Message.Text = "·车丝机#" + i + "车丝支数输入有误";
                    return;
                }
                if (billid == null)
                {
                    chesifield += "[车丝" + i + "],";
                    chesivalue += chesi[i - 1] + ",";
                }
                else
                {
                    chesivalue += "[车丝" + i + "]=" + chesi[i - 1]+",";
                }
            }
            for (i = 1; i <= 20; i++)
            {
                if (!int.TryParse(((TextBox)this.FindControl("CheFei" + i)).Text, out chefei[i - 1]))
                {
                    this.Message.Text = "·车丝机#" + i + "车丝废品支数输入有误";
                    return;
                }
                if (billid == null)
                {
                    chefeifield += "[车废" + i + "],";
                    chefeivalue += chefei[i - 1] + ",";
                }
                else
                {
                    chefeivalue += "[车废" + i + "]=" + chefei[i - 1] + ",";
                }
            }

            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "未知用户";
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            if (billid == null)
            {
                Cmd.CommandText =
                    "insert into [生产进度3]([日期],[产品类别ID],[工段],[原料投入根数],[原料投入重量],[切料支数],[粗车合格],[粗车不合格],[打字],"
                    + chesifield + chefeifield
                    + "[磁粉碳伤合格],[磁粉碳伤不合格],[磷化支数],[边角料量],[填报人])values('"
                    + riqi.ToShortDateString() + "'," + chanpin + "," + banci + "," + tourugen + "," + touruzhong + "," + qieliaozhi + "," + cuche + "," + cuchefei + "," + dazi + ","
                    + chesivalue + chefeivalue + citan + "," + citanfei + "," + linhua + "," + jiaoliao + ",'" + op + "');\n"
                    + "select @@IDENTITY";
                Cmd.CommandType = CommandType.Text;
                int newid = (int)(Decimal)Cmd.ExecuteScalar();
                ViewState["BillID"] = newid;
                //Response.Write(Cmd.CommandType);
            }
            else
            {
                Cmd.CommandText = "update [生产进度3] set "
                    + "[日期]='" + riqi.ToShortDateString() + "',"
                    + "[产品类别ID]=" + chanpin + ","
                    + "[工段]=" + banci + ","
                    + "[原料投入根数]=" + tourugen + ","
                    + "[原料投入重量]=" + touruzhong + ","
                    + "[切料支数]=" + qieliaozhi + ","
                    + "[粗车合格]=" + cuche + ","
                    + "[粗车不合格]=" + cuchefei + ","
                    + "[打字]=" + dazi + ","
                    + chesivalue + chefeivalue
                    + "[磁粉碳伤合格]=" + citan + ","
                    + "[磁粉碳伤不合格]=" + citanfei + ","
                    + "[磷化支数]=" + linhua + ","
                    + "[边角料量]=" + jiaoliao + ","
                    + "[填报人]='" + op
                    + "' where id=" + (int)billid;
                //Response.Write(Cmd.CommandText);
                Cmd.ExecuteNonQuery();
            }
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
            this.Message.Text = "·保存成功";
            this.Button_New.Visible = true;

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("JinDu3_In.aspx");
        }
    }
}
